window.onload = function () {
    initPage();
}

function initPage() {
    $("#input-info").show();
    $("#gen-idcard").hide();
}

function goBackToInput() {
    $("#input-info").show();
    $("#gen-idcard").hide();
}

function genIdCard() {
    $("#input-info").hide();
    $("#gen-idcard").show();
    var inputInfo = getInputInfo();
    showInputInfo(inputInfo);
}

function resetInputInfo() {
    $("#idNumber").val("");
    idNumberChange();
    $("#name").val("");
    $("#male").prop('checked', true);
    $("#nation").val("汉族");
    $("#birthYear").val("2020");
    $("#birthMonth").val("01");
    $("#birthDay").val("01");
    $("#address").val("");
}

function getInputInfo() {
    var idNumber = $("#idNumber").val();
    var name = $("#name").val();
    var gender = $("#male").checked ? "男" : "女";
    var nation = $("#nation").val();
    if (nation.endsWith("族")) {
        nation = nation.slice(0, nation.length - 1);
    }

    var birthYear = parseInt($("#birthYear").val());
    var birthMonth = parseInt($("#birthMonth").val());
    var birthDay = parseInt($("#birthDay").val());
    var address = $("#address").val();

    return {
        idNumber: idNumber,
        name: name,
        gender: gender,
        nation: nation,
        birthYear: birthYear,
        birthMonth: birthMonth,
        birthDay: birthDay,
        address: address
    };
}

function showInputInfo(inputInfo) {
    $("#name2").text(inputInfo.name);
    $("#gender2").text(inputInfo.gender);
    $("#nation2").text(inputInfo.nation);
    $("#birthYear2").text(inputInfo.birthYear);
    $("#birthMonth2").text(inputInfo.birthMonth);
    $("#birthDay2").text(inputInfo.birthDay);
    $("#address2").text(inputInfo.address);
    $("#idNumber2").text(inputInfo.idNumber);
}

function idNumberChange() {
    $("#idNumberLen").text($("#idNumber").val().length)
}